import axios from "axios";
import {Message} from 'element-ui';
import router from "@/router";

const instance = axios.create({
  baseURL: 'http://43.143.221.160:9010/',
  headers: {
    "Content-type": "application/json"
  },
  timeout: 10000
});
//请求拦截器
instance.interceptors.request.use(config => {
    // 判断token是否存在
    let token = localStorage.getItem('authorization-token');
    if (token) {
      config.headers['Authorization'] = 'Bearer '+token;
    }
    return config;
  }, error => {
    return Promise.reject(error);
  });
//响应拦截器
instance.interceptors.response.use(response => {
    let res_data = response.data;
    if(res_data.code!=200){
        //token过期
        if(res_data.code==401){
            localStorage.removeItem('authorization-token');
            router.push('/login');
            Message.error("当前请求发生错误")
        }
        return false;
    }
    return res_data;
  }, error => {
    return Promise.reject(error);
  });
export default instance;
